/*
Copyright 2019 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package logutil

import (
	"fmt"

	"vitess.io/vitess/go/vt/log"
)

// ConsoleLogger is a Logger that uses glog directly to log, at the right level.
//
// Note that methods on ConsoleLogger must use pointer receivers,
// because otherwise an autogenerated conversion method will be inserted in the
// call stack when ConsoleLogger is used via TeeLogger, making the log depth
// incorrect.
type ConsoleLogger struct{}

// NewConsoleLogger returns a simple ConsoleLogger.
func NewConsoleLogger() *ConsoleLogger {
	return &ConsoleLogger{}
}

// Infof is part of the Logger interface
func (cl *ConsoleLogger) Infof(format string, v ...any) {
	cl.InfoDepth(1, fmt.Sprintf(format, v...))
}

// Warningf is part of the Logger interface
func (cl *ConsoleLogger) Warningf(format string, v ...any) {
	cl.WarningDepth(1, fmt.Sprintf(format, v...))
}

// Errorf is part of the Logger interface
func (cl *ConsoleLogger) Errorf(format string, v ...any) {
	cl.ErrorDepth(1, fmt.Sprintf(format, v...))
}

// Errorf2 is part of the Logger interface
func (cl *ConsoleLogger) Errorf2(err error, format string, v ...any) {
	cl.ErrorDepth(1, fmt.Sprintf(format+": %+v", append(v, err)))
}

// Error is part of the Logger interface
func (cl *ConsoleLogger) Error(err error) {
	cl.ErrorDepth(1, fmt.Sprintf("%+v", err))
}

// Printf is part of the Logger interface
func (cl *ConsoleLogger) Printf(format string, v ...any) {
	fmt.Printf(format, v...)
}

// InfoDepth is part of the Logger interface.
func (cl *ConsoleLogger) InfoDepth(depth int, s string) {
	log.InfoDepth(1+depth, s)
}

// WarningDepth is part of the Logger interface.
func (cl *ConsoleLogger) WarningDepth(depth int, s string) {
	log.WarningDepth(1+depth, s)
}

// ErrorDepth is part of the Logger interface.
func (cl *ConsoleLogger) ErrorDepth(depth int, s string) {
	log.ErrorDepth(1+depth, s)
}
